/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dusin.teachassistdbservice.dao4mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.dusin.teachassistdbservice.dao.TeachClassDao;
import org.dusin.teachassistdbservice.dao.TeacherDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

/**
 *
 * @author dusin
 */
public class TeachClassDaoImpl implements TeachClassDao{
    static Logger logger = Logger.getLogger(TeachClassDaoImpl.class);
    private JdbcTemplate jdbcTemplate;

    public TeachClassDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    //返回自增长ID
    @Override
    public long doCreate(final Object[] bean) {
        long flag = -1;
        final String sql = "insert into teachclass(teachclassno,coursename,coursesemester,credit,evalmethod,teacherno) values(?,?,?,?,?,?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
     
        int rows = jdbcTemplate.update(new PreparedStatementCreator(){

            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                    throws SQLException {
                 PreparedStatement ps = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);                 
                 ps.setString(1, (String)bean[0]);
                 ps.setString(2, (String)bean[1]);
                 ps.setString(3, (String)bean[2]);
                 ps.setString(4, (String)bean[3]);
                 ps.setString(5, (String)bean[4]);
                 ps.setString(6, (String)bean[5]);
                 return ps;
            }
        }, keyHolder);
        
        if (rows > 0) {
            flag = keyHolder.getKey().longValue();
        }
        return flag;
    }

    @Override
    public boolean doDeleteByNo(String no,String semester) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    @Override
    public boolean doDeleteById(int id) {
        boolean flag = true;
        String sql = null;
        sql = "delete from teachclass where id = ?";
        int rows = jdbcTemplate.update(sql, id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdate(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "update teachclass set teachclassno=?,coursename=?,coursesemester=?,credit=?,evalmethod=?  where id = ?";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public Map<String, Object> findByNo(String no,String semester) {
        String sql = null;
        sql = "select * from teachclass where teachclassno=? and coursesemester=?";
        return jdbcTemplate.queryForMap(sql,no,semester);
    }
    
    @Override
    public List<Map<String, Object>> findAllClass(String teacherNo,String semester) {
        String sql = null;
        sql = "select id, teachclassno, coursename,coursesemester,credit,evalmethod from teachclass where teacherno=? and coursesemester=?";
        return jdbcTemplate.queryForList(sql,teacherNo,semester);
    }

    @Override
    public List<Map<String, Object>> findAllClass4Student(String studentNo,String semester) {
        String sql = null;
        sql = "select id, teachclassno, coursename,coursesemester,credit,evalmethod,teacherno from teachclass where  id in (select classid from studentclass where studentno=?) and coursesemester=?";
        return jdbcTemplate.queryForList(sql,studentNo,semester);
    }
    
    @Override
    public List<Map<String, Object>> findAll() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Map<String, Object>> findAll(int start, int size) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
